<template>
	<view class="content poRel">
		<view style="height: 410rpx;"></view>
		<view class="dis disAl disJuC">
			<image :src="iconimg" style="width: 219rpx;height: 219rpx;"></image>
		</view>
		<view class="textCen foSi40 fowe800 mar-top10">{{title}}</view>
		<view class="wid90 mar" style="margin-top: 108rpx;">
			<view @click="xieyiClick()" class="dis disAl disJuC foSi25 hei86" style="margin-top: 50rpx;">
				<view v-if="xieyi" class="dis disAl disJuC borRad50"  :style="'border: 1rpx solid '+theme.main_color+';'"
					style="width: 26rpx;height: 26rpx;">
					<view class="borRad50" :style="'background-color: '+theme.main_color+';'"
						style="width: 14rpx;height: 14rpx;"></view>
				</view>
				<view v-if="!xieyi" class="dis disAl disJuC borRad50" style="width: 26rpx;height: 26rpx;border: 1rpx solid #818181;"></view>
				<view class="mar-left10" style="color: #7A7A7A;">
					<span>我已阅读</span>
					<span :style="'color: '+theme.main_color+';'" @click.stop="tiaozhuan('/pages/common/richtext?type=3')">《用户协议》</span>
					<span>和</span>
					<span :style="'color: '+theme.main_color+';'" @click.stop="tiaozhuan('/pages/common/richtext?type=1')">《隐私协议》</span>
				</view>
			</view>
			<view class="borRad40 dis disAl disJuC foSi35 coFFF" @click="getOpenID" :style="'background-color: '+theme.main_color+';'"
				style="height: 100rpx;margin-top: 80rpx;">
				<image src="../../static/weixin2.png" style="width: 50rpx;height: 41rpx;"></image>
				<view class="mar-left20">微信登录</view>
			</view>
			<!-- <view class="mar-top20 textCen foSi20" style="color: #737373;" @click="tiaozhuan('/pages/login/login')">手机号登录</view> -->
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url: this.global.url,
				xieyi: false,
				iconimg: '',
				title: '',
				theme: null
			}
		},
		onLoad() {
			var that = this;
			that.theme = uni.getStorageSync('theme')
			that.iconimg = uni.getStorageSync('platform').iconimg
			that.title = uni.getStorageSync('platform').title
			// #ifdef H5
			var option = that.getUrlCode()
			if(option.code){
				uni.setStorageSync('code', option.code)
			}else{
				this.getWxCode()
			}
			// #endif
		},
		methods: {
			getWxCode(){
				var that = this
				// if(!that.xieyi){return uni.showToast({title: '请勾选用户协议',icon: 'error',duration: 2000})}
				uni.request({
					url: that.global.url+'api/qianyu_smfw/User/getCode',
					method: 'POST',
					header:{
						'site-id': that.global.site_id//自定义请求头信息
					},
					data: {},
					success(res) {
					    let result = res.data
					    if(result.code == 1){
							// 有style的写法
							// console.log(result.data);
							let appid = result.data; //为测试号id
							let local = window.location.href;
							// 页面A1 首次授权
							window.location.href =
								`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`;
						}else{
					        uni.showToast({
					            title: result.msg,
					            icon:'none',
								duration: 2000
					        })
					    }
					}
				})
			},
			getUrlCode() {
				// 截取url中的code方法
				var url = location.search;
				var theRequest = new Object();
				if (url.indexOf("?") != -1) {
					var str = url.substr(1);
					var strs = str.split("&");
					for (var i = 0; i < strs.length; i++) {
						theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
					}
				}
				return theRequest;
			},
			getOpenID(){
				var that = this
				var source = 'h5'
				// #ifdef APP
				source = 'app'
				// #endif
				
				if(!that.xieyi){return uni.showToast({title: '请勾选用户协议',icon: 'error',duration: 2000})}
				uni.request({
					url: that.global.url+'api/qianyu_smfw/user/wx_login',
					method: 'POST',
					header:{
						'site-id': that.global.site_id,//自定义请求头信息
					},
					data: {
						wx_code: uni.getStorageSync('code'),
						source: source,
						distr_id: uni.getStorageSync('distr_id'),
						agent_id: uni.getStorageSync('agent_id')
					},
					success(res) {
					    let result = res.data
					    if(result.code == 1){
							console.log(result.data);
							uni.setStorageSync('token',result.data)
				            uni.switchTab({
				            	url: '/pages/index/index'
				            })
						}else{
					        uni.showToast({
					            title: result.msg,
					            icon:'none',
								duration: 2000
					        })
					    }
					}
				})
			},
			xieyiClick(){
				this.xieyi = !this.xieyi
			},
			tiaozhuan(url) {
				uni.redirectTo({
					url: url
				})
			}
		}
	}
</script>

<style>
	.content {
		min-height: 100vh;
	}
</style>